package com.tool.finances.lifestyle;

import android.app.AlertDialog;
import android.app.TabActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.TabHost;

public class FinanceManager extends TabActivity {

	private TabHost host;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		TabStart();
		
		
	}

	@Override
	public boolean dispatchKeyEvent(KeyEvent event) {
		if (KeyEvent.KEYCODE_BACK == event.getKeyCode()
				&& event.getAction() == KeyEvent.ACTION_DOWN) {
			if (!host.getCurrentTabTag().equals(TAB_ACCOUNT)) {
				host.setCurrentTabByTag(TAB_ACCOUNT);
			} else {
				showExitDialog();
			}
			return true;
		}
		return super.dispatchKeyEvent(event);
	}

	public static final String TAB_ACCOUNT = "收支";
	public static final String TAB_STATISTICS = "统计";
	public static final String TAB_SETTING = "设置";

	private void TabStart() {
		host = getTabHost();
		// host.set
		host.addTab(host
				.newTabSpec(TAB_ACCOUNT)
				.setIndicator(TAB_ACCOUNT)
				.setContent(
						new Intent(FinanceManager.this, AccountActivity.class)));

		host.addTab(host
				.newTabSpec(TAB_STATISTICS)
				.setIndicator(TAB_STATISTICS)
				.setContent(
						new Intent(FinanceManager.this,
								StatisticsActivity.class)));

		host.addTab(host.newTabSpec(TAB_SETTING).setIndicator(TAB_SETTING)
				.setContent(new Intent(FinanceManager.this, Setting.class)));
	}

	private void showExitDialog() {
		new AlertDialog.Builder(this)
				.setTitle(getResources().getString(R.string.app_name))
				.setMessage(getResources().getString(R.string.exit_app))
				.setPositiveButton(
						getResources().getString(R.string.dialog_cancel), null)
				.setNegativeButton(
						getResources().getString(R.string.dialog_confrim),
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								finish();
							}
						}).show();
	}
}